<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-if="define.state != 1"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['datamodel']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['datamodel']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="filteredData"
      @selection-change="handleSelectionChange"
      :filter-method="filterHandler"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="标识" align="center" prop="code" />
      <el-table-column label="名称" align="center" prop="title" />
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.lc_datamodel_column_type"
            :value="scope.row.type"
          />
        </template>
      </el-table-column>
      <el-table-column label="长度" align="center" prop="lengthM" />
      <el-table-column label="小数点" align="center" prop="lengthD" />
      <el-table-column label="字段属性" align="center" prop="attr">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.lc_datamodel_attr"
            :value="scope.row.attr"
          />
        </template>
      </el-table-column>
      <el-table-column label="匹配表" align="center" prop="mapping" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        v-if="define.state != 1"
      >
        <template slot="header">
          <el-input
            v-model="filterText"
            size="mini"
            placeholder="输入关键字搜索"
          />
        </template>
        <template slot-scope="scope">
          <el-button
            :disabled="scope.row.attr == 1"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['datamodel']"
            >修改</el-button
          >
          <el-button
            :disabled="scope.row.attr == 1"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['datamodel']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改数据建模列对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="560px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="标识" prop="code">
          <el-input
            :disabled="readOnly.formCode"
            v-model="form.code"
            placeholder="请输入标识"
            @input="
              (e) => {
                form.code = e.toUpperCase().replace(/^\s+|\s+$/g, '');
              }
            "
          />
        </el-form-item>
        <el-form-item label="名称" prop="title">
          <el-input
            :disabled="readOnly.formTitle"
            v-model="form.title"
            placeholder="请输入名称"
          />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type">
            <el-option
              v-for="item in dict.type.lc_datamodel_column_type"
              :key="item.value"
              :label="item.label + '(' + item.value + ')'"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="长度" prop="lengthM">
          <el-input
            :disabled="readOnly.formLengthM"
            v-model="form.lengthM"
            placeholder="请输入长度"
          />
        </el-form-item>
        <el-form-item label="小数点" prop="lengthD">
          <el-input
            :disabled="readOnly.formLengthD"
            v-model="form.lengthD"
            placeholder="请输入小数点"
          />
        </el-form-item>
        <el-form-item label="匹配类型" prop="mappingType">
          <el-select
            v-model="form.mappingType"
            :disabled="readOnly.formMappingType"
          >
            <el-option
              v-for="item in dict.type.lc_datamodel_column_mapping_type"
              :key="item.value"
              :label="item.label + '(' + item.value + ')'"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="匹配表或字典标识" prop="mapping">
          <el-input
            :disabled="readOnly.formMapping"
            v-model="form.mapping"
            placeholder="请输入匹配表或字典标识"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  deleteColumnByCode,
  listColumns,
  getLcDatamodelColumn,
  saveColumns,
  getDefine,
} from "@/api/lc/lcDatamodelDefine";

export default {
  name: "LcDatamodelColumn",
  dicts: [
    "lc_datamodel_attr",
    "lc_datamodel_column_type",
    "lc_datamodel_column_mapping_type",
  ],
  data() {
    return {
      readOnly: {
        formCode: false,
        formTitle: false,
        formLengthM: false,
        formLengthD: false,
        formMapping: false,
        formMappingType: false,
      },

      //数据建模定义code
      defineCode: null,
      define: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      editing: false,
      // 数据建模列表格数据
      LcDatamodelColumnList: [],
      filteredData: [],
      filterText: "过滤关键字",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        title: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [
          { required: true, message: "请输入字段标识", trigger: "blur" },
          {
            min: 1,
            max: 60,
            message: "长度在 3 到 60 个字符",
            trigger: "blur",
          },
        ],
        title: [
          { required: true, message: "请输入字段名称", trigger: "blur" },
          {
            min: 1,
            max: 60,
            message: "长度在 3 到 60 个字符",
            trigger: "blur",
          },
        ],
        type: [{ required: true, message: "请选择字段类型", trigger: "blur" }],
      },
    };
  },
  watch: {
    filterText(val) {
      this.filterHandler();
    },
    "form.type"(val) {
      if (
        val == "1" ||
        val == "3" ||
        val == "4" ||
        val == "5" ||
        val == "7" ||
        val == "8"
      ) {
        // bigint, . int. boolean. char
        this.readOnly = {
          formCode: false,
          formTitle: false,
          formLengthM: true,
          formLengthD: true,
          formMapping: true,
          formMappingType: true, //匹配类型
        };
      } else if (val == "2") {
        //varchar
        this.readOnly = {
          formCode: false,
          formTitle: false,
          formLengthM: false,
          formLengthD: true,
          formMapping: false,
          formMappingType: false, //匹配类型
        };
      } else if (val == "6") {
        //decimal
        this.readOnly = {
          formCode: false,
          formTitle: false,
          formLengthM: false,
          formLengthD: false,
          formMapping: true,
          formMappingType: false, //匹配类型
        };
      } else if (val == "9") {
        //multiple
        this.readOnly = {
          formCode: false,
          formTitle: false,
          formLengthM: true,
          formLengthD: true,
          formMapping: false,
          formMappingType: false, //匹配类型
        };
      }
    },
  },
  created() {
    this.defineId = this.$route.params && this.$route.params.defineId;
    getDefine(this.defineId).then((response) => {
      this.defineCode = response.data.code;
      this.define = response.data;
      this.getList();
    });
  },
  methods: {
    //过滤函数
    filterHandler() {
      const filterText = this.filterText.toLowerCase();
      this.filteredData = this.LcDatamodelColumnList.filter((item) => {
        return Object.values(item).some((value) =>
          String(value).toLowerCase().includes(filterText)
        );
      });
    },

    /** 查询数据建模列列表 */
    getList() {
      this.loading = true;
      listColumns(this.defineCode).then((response) => {
        this.LcDatamodelColumnList = response.data;
        this.filteredData = [...this.LcDatamodelColumnList];
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        defineCode: this.defineCode,
        state: null,
        code: null,
        title: null,
        type: null,
        lengthM: null,
        lengthD: null,
        pkey: null,
        nullable: null,
        attr: null,
        mappingType: null,
        mapping: null,
        orderNum: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.editing = false;
      this.title = "添加数据建模列";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.editing = true;
      getLcDatamodelColumn(row.defineCode, row.code).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改数据建模列";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          saveColumns(this.form).then((response) => {
            if (this.editing) {
              this.$modal.msgSuccess("修改成功");
            } else {
              this.$modal.msgSuccess("新增成功");
            }
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm("确认删除？")
        .then(function () {
          return deleteColumnByCode(row.defineCode, row.code);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "lc/datamodel/define/exportColumns",
        {
          code: this.defineCode,
        },
        `LcDatamodelColumn_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
